package com.atxwq.shop.manager.serivce.impl;

import cn.hutool.core.date.DateUtil;
import com.atxwq.shop.manager.mapper.OrderInfoMapper;
import com.atxwq.shop.manager.mapper.OrderStatisticsMapper;
import com.atxwq.shop.manager.serivce.OrderInfoService;
import com.atxwq.shop.model.dto.order.OrderStatisticsDto;
import com.atxwq.shop.model.entity.order.OrderStatistics;
import com.atxwq.shop.model.vo.order.OrderStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 查询订单
 * @Date 2025/5/16 13:53
 * @Created by 12336
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    /**
     * 查询订单统计
     * @param orderStatisticsDto
     * @return
     */
    @Override
    public OrderStatisticsVo getOrderStatistics(OrderStatisticsDto orderStatisticsDto) {
        //1. 查询统计结果数据
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectList(orderStatisticsDto);
        //2.日期列表：使用stream流处理
        List<String> dateList = orderStatisticsList.stream().map(orderStatistics
                ->  DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd")).collect(Collectors.toList());
        //3.金额列表
        List<BigDecimal> amountList = orderStatisticsList.stream().map(OrderStatistics::getTotalAmount).collect(Collectors.toList());
        //4.创建OrderStatisticsVo对象并返回数据
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(amountList);

        return orderStatisticsVo;

    }
}
